package com.v2future.sjt.collegecheckingattendance.utils;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicLong;

@SuppressLint({ "SimpleDateFormat", "InlinedApi" })
public class CalculateTime {
	private static AtomicLong offSetTime = new AtomicLong();
	/** 日期修改设置默认值 */
	public static final int DEFAULT_VALUE = -1;
	/** 输出日期格式：yyyy-MM-ddHH:mm:ss */
	public static final int TIME_FORMAT_TYPE = 1;
	/** 输出日期格式：yyyy-MM-dd */
	public static final int DATE_FORMAT_TYPE = 2;
	/** 输出日期格式：yyyy-MM-dd00:00:00 */
	public static final int DAY_START_FORMAT_TYPE = 3;
	/** 输出日期格式：HH:mm:ss */
	public static final int TIME_START_FORMAT_TYPE = 4;
	/** 输出日期格式：yyyy-MM-ddHH:mm:ss */
	public static final String TIME_FORMAT = "yyyy-MM-ddHH:mm:ss";
	/** 输出日期格式：yyyy-MM-dd */
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	/** 输出日期格式：yyyy-MM-dd00:00:00 */
	public static final String DAY_START_FORMAT = "yyyy-MM-dd00:00:00";
	/** 输出日期格式：HH:mm:ss */
	public static final String TIME_START_FORMAT = "HH:mm:ss";
	public static final long SYNC_TIME_PERHOUR = 1000 * 3600;

	/** 日历返回的日期对应设计要求值：周一：1 周二：2 周三3， 周四：4 周五：5 周六：6，周日：7 */
	// public static final int[] week = { 7, 1, 2, 3, 4, 5, 6 };

	@SuppressLint("SimpleDateFormat")
	public synchronized static SimpleDateFormat getInstance(int type) {
		SimpleDateFormat simpleDateFormat = null;
		switch (type) {
		case TIME_FORMAT_TYPE:
			simpleDateFormat = new SimpleDateFormat(TIME_FORMAT);
			break;
		case DATE_FORMAT_TYPE:
			simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
			break;
		case DAY_START_FORMAT_TYPE:
			simpleDateFormat = new SimpleDateFormat(DAY_START_FORMAT);
			break;
		case TIME_START_FORMAT_TYPE:
			simpleDateFormat = new SimpleDateFormat(TIME_START_FORMAT);
			break;

		}
		return simpleDateFormat;
	}

	private CalculateTime() {
		super();
	}

	/**
	 * 
	 * 在入参日期基础上，加上入参的年，月，日，返回新的日期<br>
	 * 
	 * @param oldDate
	 *            yyy-MM-dd
	 * @param year
	 * @param mouth
	 * @param day
	 * @return yyy-MM-dd
	 */
	public static String getNewDateString(String oldDate, int year, int mouth,
										  int day) {
		GregorianCalendar cl = new GregorianCalendar();
		cl.setFirstDayOfWeek(GregorianCalendar.MONDAY); // 设置每周的第一天为星期一
		cl.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);// 每周从周一开始

		try {
			cl.setTime(getInstance(DATE_FORMAT_TYPE).parse(oldDate));
		} catch (ParseException e) {

			e.printStackTrace();
		}
		cl.add(GregorianCalendar.YEAR, year);
		cl.add(GregorianCalendar.MONTH, mouth);
		cl.add(GregorianCalendar.DATE, day);
		return getInstance(DATE_FORMAT_TYPE).format(cl.getTime());
	}

	/**
	 * 
	 * 在入参日期基础上，加上入参的年，月，日，返回新的日期<br>
	 * 
	 * @param oldDate
	 *            yyy-MM-dd
	 * @param year
	 * @param mouth
	 * @param day
	 * @return yyy-MM-dd
	 */
	public static String getNewDateStringforeign(String oldDate, int year,
												 int mouth, int day) {
		GregorianCalendar cl = new GregorianCalendar();
		try {
			cl.setTime(getInstance(DATE_FORMAT_TYPE).parse(oldDate));
		} catch (ParseException e) {

			e.printStackTrace();
		}
		cl.add(GregorianCalendar.YEAR, year);
		cl.add(GregorianCalendar.MONTH, mouth);
		cl.add(GregorianCalendar.DATE, day);
		return getInstance(DATE_FORMAT_TYPE).format(cl.getTime());
	}

	/**
	 * 在入参日期基础上，设置入参时间域和值，返回新的日期<br>
	 * 
	 * @param oldDate
	 *            yyy-MM-dd
	 * @param field
	 * @param value
	 * @return yyy-MM-dd
	 */
	public static String getNewDateString(String oldDate, int field, int value) {
		GregorianCalendar cl = new GregorianCalendar();
		cl.setFirstDayOfWeek(GregorianCalendar.MONDAY); // 设置每周的第一天为星期一
		cl.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);// 每周从周一开始
		try {
			cl.setTime(getInstance(DATE_FORMAT_TYPE).parse(oldDate));
		} catch (ParseException e) {

			e.printStackTrace();
		}
		cl.add(field, value);
		return getInstance(DATE_FORMAT_TYPE).format(cl.getTime());
	}

	/**
	 * 返回給定日期是星期几，按每周第一天为周一处理，周一：1，周二：2.。。。
	 * 
	 * @param oldDate
	 *            yyy-MM-dd
	 * @return
	 */
	public static int getDayOfWeek(String oldDate) {
		GregorianCalendar cl = new GregorianCalendar();
		cl.setFirstDayOfWeek(GregorianCalendar.MONDAY); // 设置每周的第一天为星期一
		cl.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);// 每周从周一开始
		try {
			cl.setTime(getInstance(DATE_FORMAT_TYPE).parse(oldDate));
		} catch (ParseException e) {

			e.printStackTrace();
		}
		return cl.get(GregorianCalendar.DAY_OF_WEEK);

	}

	public static String getWeekByDay(String date) {
		GregorianCalendar cl = new GregorianCalendar();
		cl.setFirstDayOfWeek(GregorianCalendar.MONDAY); // 设置每周的第一天为星期一
		cl.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);// 每周从周一开始
		try {

			SimpleDateFormat dateFm = new SimpleDateFormat("E");
			return dateFm.format(getInstance(DATE_FORMAT_TYPE).parse(date));
		} catch (Exception e) {

		}
		return "";

	}

	/**
	 * 
	 * 入参日期，设置年月日，可设置默认
	 * 
	 * @param oldDate
	 *            yyy-MM-dd
	 * @param year
	 * @param month
	 * @param day
	 * @return yyy-MM-dd
	 */
	public static String getDateSetUpString(String oldDate, int year,
											int month, int day) {
		GregorianCalendar cl = new GregorianCalendar();
		cl.setFirstDayOfWeek(GregorianCalendar.MONDAY); // 设置每周的第一天为星期一
		cl.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);// 每周从周一开始
		try {
			cl.setTime(getInstance(DATE_FORMAT_TYPE).parse(oldDate));
		} catch (ParseException e) {

			e.printStackTrace();
		}
		if (year != DEFAULT_VALUE) {
			cl.set(GregorianCalendar.YEAR, year);
		}
		if (month != DEFAULT_VALUE) {
			cl.set(GregorianCalendar.MONTH, month - 1);
		}
		if (day != DEFAULT_VALUE) {
			cl.set(GregorianCalendar.DAY_OF_MONTH, day);
		}
		return getInstance(DATE_FORMAT_TYPE).format(cl.getTime());
	}

	/**
	 * 入参日期，设置时间域，可设置默认
	 * 
	 * @param oldDate
	 *            yyy-MM-dd
	 * @param field
	 * @param value
	 * @return yyy-MM-dd
	 */
	public static String getDateSetUpFieldString(String oldDate, int field,
												 int value) {
		GregorianCalendar cl = new GregorianCalendar();
		cl.setFirstDayOfWeek(GregorianCalendar.MONDAY); // 设置每周的第一天为星期一
		cl.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);// 每周从周一开始
		try {
			cl.setTime(getInstance(DATE_FORMAT_TYPE).parse(oldDate));
		} catch (ParseException e) {

			e.printStackTrace();
		}
		cl.set(field, value);
		return getInstance(DATE_FORMAT_TYPE).format(cl.getTime());
	}

	/**
	 * 入参日期运算时间值，正数，返回之后的日期，负数返回之前的日期
	 * 
	 * @param time1
	 *            yyyy-MM-ddHH:mm:ss
	 * @param second
	 * @return yyyy-MM-ddHH:mm:ss
	 */
	public static String getbeforeTimeFromGive(String time1, int second) {
		try {
			Date date = getInstance(TIME_FORMAT_TYPE).parse(time1);

			date.setTime(date.getTime() - second * 1000 + offSetTime.get());
			return getInstance(TIME_FORMAT_TYPE).format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 比较两个日期前后
	 * @param DATE1 日期1
	 * @param DATE2 日期2
     * @return 1:d1在d2之前
	 * -1:d1在d2之后
	 * 0:日期相等
     */
	public static int compare_date(String DATE1, String DATE2) {


		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return -1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return 1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取当天开始UTC
	 * 
	 * @return
	 */
	public static long getDayStartMS() {
		return getMS(getInstance(DAY_START_FORMAT_TYPE).format(new Date()));

	}

	/**
	 * 获取明天开始UTC
	 * 
	 * @return
	 */
	public static long getTomorrowstartMS() {

		GregorianCalendar cl = new GregorianCalendar();
		cl.setTime(new Date());
		cl.add(GregorianCalendar.DATE, +1);
		return getMS(getInstance(DAY_START_FORMAT_TYPE).format(cl.getTime()));
	}

	/**
	 * 秒为单位
	 * 
	 * @return
	 */
	public static int getYesterdayNoon() {
		Calendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, -1);

		currentDate.set(Calendar.HOUR_OF_DAY, 12);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		return (int) (currentDate.getTime().getTime() / 1000);
	}

	/**
	 * 秒为单位
	 * 
	 * @return
	 */
	public static int getTodayStartTime() {
		Calendar currentDate = new GregorianCalendar();

		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		return (int) (currentDate.getTime().getTime() / 1000);
	}

	public static int getTodayEndTime() {
		Calendar currentDate = new GregorianCalendar();

		currentDate.set(Calendar.HOUR_OF_DAY, 23);
		currentDate.set(Calendar.MINUTE, 59);
		currentDate.set(Calendar.SECOND, 59);
		return (int) (currentDate.getTime().getTime() / 1000);
	}

	/**
	 * 返回UTC毫秒值
	 * 
	 * @param time
	 *            yyyy-MM-ddHH:mm:ss
	 * @return
	 */
	public static long getMS(String time) {
		try {
			Date date = getInstance(TIME_FORMAT_TYPE).parse(time);
			return date.getTime();

		} catch (Exception e) {
			LogUtils.d(e.toString());
		}
		return -1;

	}

	/**
	 * 返回UTC毫秒值
	 * 
	 * @param time
	 *            HH:mm:ss
	 * @return
	 */
	public static long getMS2(String time) {
		try {
			Date date = getInstance(TIME_START_FORMAT_TYPE).parse(time);
			return date.getTime();

		} catch (Exception e) {
			LogUtils.d(e.toString());
		}
		return -1;

	}

	/**
	 * 获取对应日期
	 * 
	 * @param timeMS
	 * @return yyyy-MM-ddHH:mm:ss
	 */
	public static String getDateString(long timeMS) {
		Date date = new Date();
		date.setTime(timeMS);
		return getInstance(TIME_FORMAT_TYPE).format(date);
	}

}
